package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.example.lotterysystem.common.exception.ControllerException;
import com.example.lotterysystem.common.pojo.CommonResult;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.UserLoginPasswordParam;
import com.example.lotterysystem.controller.param.UserRegisterParam;
import com.example.lotterysystem.controller.param.ShortMessageLoginParam;
import com.example.lotterysystem.controller.result.BaseUserInfoResult;
import com.example.lotterysystem.controller.result.UserLoginResult;
import com.example.lotterysystem.controller.result.UserRegisterResult;
import com.example.lotterysystem.service.UserService;
import com.example.lotterysystem.service.VerificationCodeService;
import com.example.lotterysystem.service.dto.UserDTO;
import com.example.lotterysystem.service.dto.UserLoginDTO;
import com.example.lotterysystem.service.dto.UserRegisterDTO;
import com.example.lotterysystem.service.enums.UserIdentityEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class UserController {

    private final static Logger logger= LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private VerificationCodeService verificationCodeService;

    //用户注册
    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> userRegister(@Validated @RequestBody UserRegisterParam param){
        //将param序列化
        logger.info("用户注册请求参数param:{}", JacksonUtil.writeValueAsString(param));
        //调用service层方法完成业务逻辑
        UserRegisterDTO userRegisterDTO = userService.register(param);
        //将类型转换
        return CommonResult.success(convertToUserRegisterResult(userRegisterDTO));
    }

    @RequestMapping("/verification-code/send")
    public CommonResult<Boolean> sendVerificationCode(String phoneNumber){
        logger.info("sendVerificationCode phoneNumber：{}",phoneNumber);
        verificationCodeService.sendVerificationCode(phoneNumber);
        return CommonResult.success(Boolean.TRUE);
    }

    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> userLoginPassword(@Validated @RequestBody UserLoginPasswordParam param){
        logger.info("userLoginPassword UserLoginPasswordParam：{}",JacksonUtil.writeValueAsString(param));
        UserLoginDTO userLoginDTO=userService.login(param);
        return CommonResult.success(convertToUserUserLoginResult(userLoginDTO));
    }

    @RequestMapping("/message/login")
    public CommonResult<UserLoginResult> shortMessageLogin(@Validated @RequestBody ShortMessageLoginParam param){
        logger.info("shortMessageLogin shortMessageLoginParam：{}",JacksonUtil.writeValueAsString(param));
        UserLoginDTO userLoginDTO=userService.login(param);
        return CommonResult.success(convertToUserUserLoginResult(userLoginDTO));
    }

    @RequestMapping("/base-user/find-list")
    public CommonResult<List<BaseUserInfoResult>> findUserInfoByIdentity(String identity){
        logger.info("findUserInfoByIdentity identity:{}",identity);
        List<UserDTO> userDTOList=userService.findUserInfo(UserIdentityEnum.forName(identity));
        return CommonResult.success(convertToList(userDTOList));
    }

    private List<BaseUserInfoResult> convertToList(List<UserDTO> userDTOList) {
        if (userDTOList == null){
            //返回一个空列表
            return Arrays.asList();
        }
        return userDTOList.stream().map(userDTO -> {
            BaseUserInfoResult result =new BaseUserInfoResult();
            result.setUserName(userDTO.getUserName());
            result.setUserId(userDTO.getUserId());
            result.setIdentity(userDTO.getIdentity().name());
            return result;
        }).collect(Collectors.toList());
    }


    private UserLoginResult convertToUserUserLoginResult(UserLoginDTO userLoginDTO) {
        if (userLoginDTO == null){
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        }
        UserLoginResult result=new UserLoginResult();
        result.setToken(userLoginDTO.getToken());
        result.setIdentity(userLoginDTO.getIdentity().name());
        return result;
    }



    private UserRegisterResult convertToUserRegisterResult(UserRegisterDTO userRegisterDTO) {
        //先对userRegisterDTO进行判空处理
        if (userRegisterDTO==null){
            //抛出自定义的controller层异常
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }
        UserRegisterResult registerResult=new UserRegisterResult();
        registerResult.setUserId(userRegisterDTO.getUserId());
        return registerResult;
    }
}
